﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;


namespace PlanningGIS.Product
{
    /// <summary>
    /// 元数据操作辅助类
    /// 徐红波
    /// 20121115
    /// </summary>
    [Program("元数据操作辅助类", "", "元数据")]
    [Developer("徐红波", "2012-11-15", "第一技术服务部")]
    public class MetadataHelper
    {

        /// <summary>
        /// 从元数据要素类中读取指定类型元数据要素
        /// </summary>
        /// <param name="pMetadataType"></param>
        /// <param name="pFeatureclass"></param>
        /// <returns></returns>
        public static List<PlanningGISMetadata> getMetadataFromFeatureclass(Type pMetadataType, IFeatureClass pFeatureclass)
        {
            List<PlanningGISMetadata> metadatas = new List<PlanningGISMetadata>();

            List<string> fieldNames = CommonClass.getFields(pMetadataType, BindingFlags.Public);
            IQueryFilter queryFilter = new QueryFilterClass();
            IFeatureCursor featurecusor= pFeatureclass.Search(queryFilter,false);
            PlanningGISMetadata metadata =null;
            //string note = "";
            bool isCorrect = true;
            //System.Reflection.Assembly asm ;

            if (featurecusor !=null)
            {
                IFeature feature = featurecusor.NextFeature();

                while (feature!=null)
                {
                    if (pMetadataType.Equals(typeof(PlanningGISDatabaseMetadata))) { metadata = new PlanningGISDatabaseMetadata(); }
                    else if (pMetadataType.Equals(typeof(PlanningGISDLGRegionMetadata))) { metadata = new PlanningGISDLGRegionMetadata(); }
                    else if (pMetadataType.Equals(typeof(PlanningGISDOMRegionMetadata))) { metadata = new PlanningGISDOMRegionMetadata(); }
                    else if (pMetadataType.Equals(typeof(PlanningGISDEMRegionMetadata))) { metadata = new PlanningGISDEMRegionMetadata(); }
                    else if (pMetadataType.Equals(typeof(PlanningGISMapsheetMetadata))) { metadata = new PlanningGISMapsheetMetadata(); }
                    else if (pMetadataType.Equals(typeof(PlanningGISFeatureMetadata))) { metadata = new PlanningGISFeatureMetadata(); }
                    else { metadata = null; }

                    if (metadata!=null)
                    {
                        foreach(string fieldName in fieldNames)
                        {
                            FieldInfo fieldInfo = pMetadataType.GetField(fieldName);

                            if (pFeatureclass.FindField(fieldName) != -1)
                            {
                                if (fieldInfo.FieldType == typeof(IGeometry))
                                {
                                    if (pFeatureclass.Fields.get_Field(pFeatureclass.FindField(fieldName)).Type != esriFieldType.esriFieldTypeGeometry)
                                    {
                                        isCorrect = false;
                                    }
                                    else
                                    {
                                        pMetadataType.InvokeMember(fieldName, BindingFlags.SetField, null, metadata, new object[] { feature.Shape});
                                       
                                    }
                                }
                                else if (fieldInfo.FieldType == typeof(String))
                                {
                                    pMetadataType.InvokeMember(fieldName
                                        , BindingFlags.SetField
                                        , null
                                        , metadata
                                        , new object[] { feature.get_Value(pFeatureclass.FindField(fieldName)).ToString() });
                               
                                }
                                else if (fieldInfo.FieldType == typeof(PlanningGISMetadataType))
                                {
                                    PlanningGISMetadataType metadataType= PlanningGISMetadataType.DATABASE_LEVEL;
                                    bool ifHasValue = true;

                                    switch (feature.get_Value(pFeatureclass.FindField(fieldName)).ToString())
                                    {
                                        case "DATABASE_LEVEL": metadataType = PlanningGISMetadataType.DATABASE_LEVEL; break;
                                        case "REGION_LEVEL_DLG": metadataType = PlanningGISMetadataType.REGION_LEVEL_DLG; break;
                                        case "REGION_LEVEL_DEM": metadataType = PlanningGISMetadataType.REGION_LEVEL_DEM; break;
                                        case "REGION_LEVEL_DOM": metadataType = PlanningGISMetadataType.REGION_LEVEL_DOM; break;
                                        case "MAPSHEET_LEVEL": metadataType = PlanningGISMetadataType.MAPSHEET_LEVEL; break;
                                        case "FEATURE_LEVEL": metadataType = PlanningGISMetadataType.FEATURE_LEVEL; break;
                                        default: ifHasValue = false; break;
                                    }

                                    if (ifHasValue ==true)
                                    {
                                        pMetadataType.InvokeMember(fieldName, BindingFlags.SetField, null, metadata, new object[] { metadataType });
                                    }
                                }
                                else
                                {
                                    Type typeTemp = fieldInfo.FieldType;
                                    //asm = typeTemp.Assembly;
                                    //String v = new String();
                                    //object objTemp=asm.CreateInstance(typeTemp.FullName);
                                    object objTemp;

                                    if (fieldInfo.FieldType == typeof(Double))
                                    {
                                        Double value = 0; objTemp = value;
                                    }
                                    else if (fieldInfo.FieldType == typeof(float))
                                    {
                                        float value = 0; objTemp = value;
                                    }
                                    else if (fieldInfo.FieldType == typeof(Int16))
                                    {
                                        Int16 value = 0; objTemp = value;
                                    }
                                    else if (fieldInfo.FieldType == typeof(Int32))
                                    {
                                        Int32 value = 0; objTemp = value;
                                    }
                                    else if (fieldInfo.FieldType == typeof(Int64))
                                    {
                                        Int64 value = 0; objTemp = value;
                                    }
                                    else if (fieldInfo.FieldType == typeof(DateTime))
                                    {
                                        DateTime value = DateTime.Now; objTemp = value;
                                    }
                                    //else if (fieldInfo.FieldType == typeof(String))
                                    //{
                                    //    String value=""; objTemp = value;
                                    //}
                                    else if (fieldInfo.FieldType == typeof(long))
                                    {
                                        long value = 0; objTemp = value;
                                    }
                                    else
                                    {
                                        objTemp = new object();
                                    }
                                    //else if (fieldInfo.FieldType == typeof(float))
                                    //{
                                    //    float value; objTem = value;
                                    //}
                                    //else if (fieldInfo.FieldType == typeof(float))
                                    //{
                                    //    float value; objTem = value;
                                    //}
                                    //object objTemp 
                                    //    = typeTemp.InvokeMember(typeTemp.Name
                                    //    ,BindingFlags.Public 
                                    //    | BindingFlags.CreateInstance 
                                    //    | BindingFlags.Instance
                                    //    , null
                                    //    , null
                                    //    , new object[] { });
                                    object canParse
                                        = typeTemp.InvokeMember("TryParse"
                                        , BindingFlags.InvokeMethod
                                        , null
                                        , objTemp
                                        , new object[] { feature.get_Value(pFeatureclass.FindField(fieldName)).ToString(), objTemp });

                                    if ((bool)canParse == true)
                                    {
                                        pMetadataType.InvokeMember(fieldName, BindingFlags.SetField, null, metadata, new object[] { objTemp });
                                    }
                                    else
                                    {
                                        isCorrect = false;
                                    }
                                }

                                
                                //else if (fieldInfo.FieldType == typeof(DateTime))
                                //{
                                //    DateTime dt = new DateTime();

                                //    if (DateTime.TryParse(feature.get_Value(pFeatureclass.FindField(fieldName)).ToString(), out dt) == true)
                                //    {
                                //        pMetadataType.InvokeMember(fieldName, BindingFlags.SetField, null, metadata, new object[] { dt });
                                //    }
                                //    else
                                //    {
                                //        isCorrect = false;
                                //    }
                                //}
                                //else if (fieldInfo.FieldType == typeof(Double))
                                //{
                                //    Double value = new Double();

                                //    if (Double.TryParse(feature.get_Value(pFeatureclass.FindField(fieldName)).ToString(), out value) == true)
                                //    {
                                //        pMetadataType.InvokeMember(fieldName, BindingFlags.SetField, null, metadata, new object[] { value });
                                //    }
                                //    else
                                //    {
                                //        isCorrect = false;
                                //    }
                                //}
                            }
                            else
                            {
                                isCorrect = false;
                            }

                           
                           
                        }

                        metadatas.Add(metadata);
                    }

                    feature = featurecusor.NextFeature();
                }
            }
            
            return metadatas;
        }

        /// <summary>
        /// 获取元数据要素类默认名称
        /// </summary>
        /// <param name="pMetadata"></param>
        /// <returns></returns>
        public static string getMetadataFeatureclassDefaultName(PlanningGISMetadataType pMetadataType, PlanningGISDatabaseType pDatabaseType)
        {
            string databaseType = Enum.GetName(typeof(PlanningGISDatabaseType), pDatabaseType);

            switch (pMetadataType)
            {
                case PlanningGISMetadataType.DATABASE_LEVEL: return "MD_" + databaseType + "_DB";
                case PlanningGISMetadataType.REGION_LEVEL_DLG: return "MD_" + databaseType + "_CQ";
                case PlanningGISMetadataType.REGION_LEVEL_DEM: return "MD_" + databaseType + "_CQ";
                case PlanningGISMetadataType.REGION_LEVEL_DOM: return "MD_" + databaseType + "_CQ";
                case PlanningGISMetadataType.MAPSHEET_LEVEL: return "MD_" + databaseType + "_TF";
                case PlanningGISMetadataType.FEATURE_LEVEL: return "MD_" + databaseType + "_FT";
            }

            return "";
        }

        /// <summary>
        /// 获取元数据要素类默认定义(根据元数据类型)
        /// </summary>
        /// <param name="pMetadataType"></param>
        /// <returns></returns>
        public static PlanningGISFeatureclass getMetadataDefaultDef(Type pType, PlanningGISDatabaseType pDatabaseType)
        {
            if (pType.IsSubclassOf(typeof(PlanningGISMetadata))==false)
            {
                return null;
            }

            PlanningGISMetadataType metadataType = PlanningGISMetadataType.DATABASE_LEVEL;
            string layerAliasName = "";

            if (pType.Equals(typeof(PlanningGISDatabaseMetadata)))
            {
                metadataType = PlanningGISMetadataType.DATABASE_LEVEL;
                layerAliasName = "数据库级元数据";
            }
            else if (pType.Equals(typeof(PlanningGISDLGRegionMetadata)))
            {
                metadataType = PlanningGISMetadataType.REGION_LEVEL_DLG;
                layerAliasName = "DLG测区级元数据";
            }
            else if (pType.Equals(typeof(PlanningGISDEMRegionMetadata)))
            {
                metadataType = PlanningGISMetadataType.REGION_LEVEL_DEM;
                layerAliasName = "DEM测区级元数据";
            }
            else if (pType.Equals(typeof(PlanningGISDOMRegionMetadata)))
            {
                metadataType = PlanningGISMetadataType.REGION_LEVEL_DOM;
                layerAliasName = "DOM测区级元数据";
            }
            else if (pType.Equals(typeof(PlanningGISMapsheetMetadata)))
            {
                metadataType = PlanningGISMetadataType.MAPSHEET_LEVEL;
                layerAliasName = "图幅级元数据";
            }
            else if (pType.Equals(typeof(PlanningGISFeatureMetadata)))
            {
                metadataType = PlanningGISMetadataType.FEATURE_LEVEL;
                layerAliasName = "要素级元数据";
            }

            //layerAliasName = Enum.GetName(typeof(PlanningGISDatabaseType), pDatabaseType) + layerAliasName;

            PlanningGISFeatureclass zjgisFeatureclass =  new PlanningGISFeatureclass();
            zjgisFeatureclass.name = getMetadataFeatureclassDefaultName(metadataType, pDatabaseType);
            zjgisFeatureclass.hasZ = false;
            zjgisFeatureclass.hasM = false;
            zjgisFeatureclass.shapeFieldName = "Shape";
            zjgisFeatureclass.geometryType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon;
            zjgisFeatureclass.featureType = esriFeatureType.esriFTSimple;
            zjgisFeatureclass.aliasName = layerAliasName;
            PlanningGISField field = null;
            string fieldNameTemp = "";

            foreach (string fieldName in CommonClass.getFields(pType, BindingFlags.Public))
            {
                // 字段名首字母大写
                field = PlanningGISMetadata.createPlanningGISFieldByFieldName(pType, fieldName);
                fieldNameTemp = fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1, fieldName.Length - 1);
                field.name = fieldNameTemp;

                if (field != null)
                {
                    zjgisFeatureclass.fields.Add(field);
                }
            }

            foreach (string fieldName in CommonClass.getProperties(pType, BindingFlags.Public))
            {
                // 字段名首字母大写
                field = PlanningGISMetadata.createPlanningGISFieldByFieldName(pType, fieldName);
                fieldNameTemp = fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1, fieldName.Length - 1);
                field.name = fieldNameTemp;

                if (field != null)
                {
                    zjgisFeatureclass.fields.Add(field);
                }
            }

            return zjgisFeatureclass;
        }

        /// <summary>
        /// 获取元数据要素类默认定义(根据元数据类型枚举)
        /// </summary>
        /// <param name="pMetadataType"></param>
        /// <returns></returns>
        public static PlanningGISFeatureclass getMetadataDefaultDef(PlanningGISMetadataType pMetadataType, PlanningGISDatabaseType pDatabaseType)
        {
            switch (pMetadataType)
            {
                case PlanningGISMetadataType.DATABASE_LEVEL: return getDatabaseMetadataDefaultDef(pDatabaseType);
                case PlanningGISMetadataType.REGION_LEVEL_DLG: return getRegionMetadataDefaultDef(PlanningGISMetadataType.REGION_LEVEL_DLG, pDatabaseType);
                case PlanningGISMetadataType.REGION_LEVEL_DEM: return getRegionMetadataDefaultDef(PlanningGISMetadataType.REGION_LEVEL_DEM, pDatabaseType);
                case PlanningGISMetadataType.REGION_LEVEL_DOM: return getRegionMetadataDefaultDef(PlanningGISMetadataType.REGION_LEVEL_DOM, pDatabaseType);
                case PlanningGISMetadataType.MAPSHEET_LEVEL: return getMapsheetMetadataDefaultDef(pDatabaseType);
                case PlanningGISMetadataType.FEATURE_LEVEL: return getFeatureMetadataDefaultDef(pDatabaseType);
            }

            return null;
        }


        /// <summary>
        /// 获取数据库级元数据要素类默认定义
        /// </summary>
        /// <returns></returns>
        public static PlanningGISFeatureclass getDatabaseMetadataDefaultDef(PlanningGISDatabaseType pDatabaseType)
        {
            PlanningGISFeatureclass metadataFCDef = new PlanningGISFeatureclass();
            metadataFCDef.name = getMetadataFeatureclassDefaultName(PlanningGISMetadataType.DATABASE_LEVEL, pDatabaseType);
            metadataFCDef.hasZ = false;
            metadataFCDef.hasM = false;
            metadataFCDef.shapeFieldName = "Shape";
            metadataFCDef.geometryType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon;
            metadataFCDef.featureType = esriFeatureType.esriFTSimple;
            metadataFCDef.aliasName = "";
            PlanningGISField field = null;
            string fieldNameTemp = "";

            foreach (string fieldName in CommonClass.getFields(typeof(PlanningGISDatabaseMetadata), BindingFlags.Public))
            {
                // 字段名首字母大写
                fieldNameTemp = fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1, fieldName.Length - 1);
                
                field = PlanningGISMetadata.createPlanningGISFieldByFieldName(typeof(PlanningGISDatabaseMetadata), fieldName);

                if (field != null)
                {
                    metadataFCDef.fields.Add(field);
                }
            }
           // metadataFCDef.fields.Add(PlanningGISField.createField("DataName", metadataFCDef.getXMLAttributeString("DataName"), esriFieldType.esriFieldTypeString, 64));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DbManageCompanyName", "数据库管理单位名称", esriFieldType.esriFieldTypeString, 64));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DataSize", "数据量", esriFieldType.esriFieldTypeString, 20));
           // metadataFCDef.fields.Add(PlanningGISField.createField("MetadataCreateTime", "元数据创建日期", esriFieldType.esriFieldTypeDate));
           // metadataFCDef.fields.Add(PlanningGISField.createField("ExpressionInfoTime", "现势性", esriFieldType.esriFieldTypeString, 64));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DQEvaluateCompanyName", "数据质量评价单位", esriFieldType.esriFieldTypeString, 64));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DQEvaluate", "数据质量评价", esriFieldType.esriFieldTypeString, 50));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DataEvaluateTime", "数据评价时间", esriFieldType.esriFieldTypeDate));
           // metadataFCDef.fields.Add(PlanningGISField.createField("UpdateType", "更新类型", esriFieldType.esriFieldTypeString, 64));
           // metadataFCDef.fields.Add(PlanningGISField.createField("UpdateCompany", "更新单位", esriFieldType.esriFieldTypeString, 64));
           // metadataFCDef.fields.Add(PlanningGISField.createField("UpdateTime", "更新时间", esriFieldType.esriFieldTypeDate));
           // metadataFCDef.fields.Add(PlanningGISField.createField("UpdateScaleDenominator", "更新比例尺分母", esriFieldType.esriFieldTypeInteger));
           // PlanningGISField geoField = new PlanningGISField();
           // geoField.name = "UpdateDomain";
           // geoField.aliasName = "更新范围";
           // geoField.fieldType = esriFieldType.esriFieldTypeGeometry;
           // ESRI.ArcGIS.Geodatabase.IGeometryDef geoDef = new GeometryDefClass();
           // IGeometryDefEdit geoDefEdit = geoDef as IGeometryDefEdit;
           // geoDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon;
           // geoDefEdit.HasM_2 = false;
           // geoDefEdit.HasZ_2 = false;
           // ISpatialReferenceFactory2 spatialRefFactory = (new SpatialReferenceEnvironment()) as ISpatialReferenceFactory2;
           // ISpatialReference spatialRef=spatialRefFactory.CreateSpatialReference(4490);
           // geoDefEdit.SpatialReference_2 = spatialRef;
           // geoField.geometryDef = geoDef;
           // metadataFCDef.fields.Add(geoField);
           // metadataFCDef.fields.Add(PlanningGISField.createField("UpdateSummary", "更新摘要", esriFieldType.esriFieldTypeString,500));
           // metadataFCDef.fields.Add(PlanningGISField.createField("RelatedRegion", "涉及测区", esriFieldType.esriFieldTypeString,500));
           // metadataFCDef.fields.Add(PlanningGISField.createField("UpdateRefences", "更新参考资料", esriFieldType.esriFieldTypeString,500));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DataFormat", "数据格式", esriFieldType.esriFieldTypeString,20));
           // metadataFCDef.fields.Add(PlanningGISField.createField("SecrecyLevel", "密级", esriFieldType.esriFieldTypeString,4));
           // metadataFCDef.fields.Add(PlanningGISField.createField("LocationSystemName", "坐标系统名称", esriFieldType.esriFieldTypeString,64));
           // metadataFCDef.fields.Add(PlanningGISField.createField("ElevationBenchmark", "高程基准", esriFieldType.esriFieldTypeString,64));
           // metadataFCDef.fields.Add(PlanningGISField.createField("LocationUnit", "坐标单位", esriFieldType.esriFieldTypeString,10));
           //metadataFCDef.fields.Add(PlanningGISField.createField("ProductOwnerCompanyName", "产品所有权单位名称", esriFieldType.esriFieldTypeString,50));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DistributionMedium", "分发介质", esriFieldType.esriFieldTypeString,20));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DistributionFormat", "分发格式", esriFieldType.esriFieldTypeString,20));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DistributionCompanyPhone", "分发单位联系电话", esriFieldType.esriFieldTypeString,20));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DistributionCompanyFax", "分发单位传真电话", esriFieldType.esriFieldTypeString,20));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DistributionCompanyPostcode", "分发单位邮政编码", esriFieldType.esriFieldTypeString,6));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DistributionCompanyName", "分发单位名称", esriFieldType.esriFieldTypeString,50));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DistributionCompanyEmail", "分发单位电子邮箱地址", esriFieldType.esriFieldTypeString,50));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DistributionCompanyWebsite", "分发单位网络地址", esriFieldType.esriFieldTypeString,50));
           // metadataFCDef.fields.Add(PlanningGISField.createField("DistributionDate", "分发日期", esriFieldType.esriFieldTypeDate));
           
            return metadataFCDef;
        }

        /// <summary>
        /// 获取测区级元数据要素类默认定义
        /// </summary>
        /// <returns></returns>
        public static PlanningGISFeatureclass getRegionMetadataDefaultDef(PlanningGISMetadataType  subRegionType,PlanningGISDatabaseType pDatabaseType)
        {
            PlanningGISFeatureclass metadataFCDef = new PlanningGISFeatureclass();
            metadataFCDef.name = getMetadataFeatureclassDefaultName(subRegionType, pDatabaseType);
            metadataFCDef.hasZ = false;
            metadataFCDef.hasM = false;
            metadataFCDef.shapeFieldName = "Shape";
            metadataFCDef.geometryType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon;
            metadataFCDef.featureType = esriFeatureType.esriFTSimple;
            metadataFCDef.aliasName = "";
            PlanningGISField field = null;
            string fieldNameTemp = "";

            foreach (string fieldName in CommonClass.getFields(typeof(PlanningGISRegionMetadata), BindingFlags.Public))
            {
                // 字段名首字母大写
                fieldNameTemp = fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1, fieldName.Length - 1);
                
                field = PlanningGISMetadata.createPlanningGISFieldByFieldName(typeof(PlanningGISRegionMetadata), fieldName);

                if (field != null)
                {
                    metadataFCDef.fields.Add(field);
                }
            }

            return metadataFCDef;
        }

        /// <summary>
        /// 获取图幅级元数据要素类默认定义
        /// </summary>
        /// <returns></returns>
        public static PlanningGISFeatureclass getMapsheetMetadataDefaultDef(PlanningGISDatabaseType pDatabaseType)
        {
            PlanningGISFeatureclass metadataFCDef = new PlanningGISFeatureclass();
            metadataFCDef.name = getMetadataFeatureclassDefaultName(PlanningGISMetadataType.MAPSHEET_LEVEL, pDatabaseType);
            metadataFCDef.hasZ = false;
            metadataFCDef.hasM = false;
            metadataFCDef.shapeFieldName = "Shape";
            metadataFCDef.geometryType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon;
            metadataFCDef.featureType = esriFeatureType.esriFTSimple;
            metadataFCDef.aliasName = "";
            PlanningGISField field = null;
            string fieldNameTemp = "";

            foreach (string fieldName in CommonClass.getFields(typeof(PlanningGISMapsheetMetadata), BindingFlags.Public))
            {
                // 字段名首字母大写
                fieldNameTemp = fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1, fieldName.Length - 1);
                
                field = PlanningGISMetadata.createPlanningGISFieldByFieldName(typeof(PlanningGISMapsheetMetadata), fieldName);

                if (field != null)
                {
                    metadataFCDef.fields.Add(field);
                }
            }

            return metadataFCDef;
        }

        /// <summary>
        /// 获取要素级元数据要素类默认定义
        /// </summary>
        /// <returns></returns>
        public static PlanningGISFeatureclass getFeatureMetadataDefaultDef(PlanningGISDatabaseType pDatabaseType)
        {
            PlanningGISFeatureclass metadataFCDef = new PlanningGISFeatureclass();
            metadataFCDef.name = getMetadataFeatureclassDefaultName(PlanningGISMetadataType.FEATURE_LEVEL, pDatabaseType);
            metadataFCDef.hasZ = false;
            metadataFCDef.hasM = false;
            metadataFCDef.shapeFieldName = "Shape";
            metadataFCDef.geometryType = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon;
            metadataFCDef.featureType = esriFeatureType.esriFTSimple;
            metadataFCDef.aliasName = "";
            PlanningGISField field = null;
            string fieldNameTemp = "";

            foreach (string fieldName in CommonClass.getFields(typeof(PlanningGISFeatureMetadata), BindingFlags.Public))
            {
                // 字段名首字母大写
                fieldNameTemp = fieldName.Substring(0, 1).ToUpper() + fieldName.Substring(1, fieldName.Length - 1);
                
                field = PlanningGISMetadata.createPlanningGISFieldByFieldName(typeof(PlanningGISFeatureMetadata), fieldName);

                if (field != null)
                {
                    metadataFCDef.fields.Add(field);
                }
            }

            return metadataFCDef;
        }

        /// <summary>
        /// 将类属性值保存到类属性中
        /// </summary>
        /// <param name="pMetadata"></param>
        /// <param name="pFieldName">类中属性名称，如不存在，则返回false</param>
        /// <param name="pFieldValue">类属性值均为字符串，具体赋值时根据属性的类型转换为相应类型，如转换失败，则返回false</param>
        /// <param name="pNote"></param>
        /// <returns></returns>
        public static bool saveMetadataItemValue(PlanningGISMetadata pMetadata, string pFieldName, string pFieldValue, out string pNote)
        {
            Type metadataType = null;

            switch (pMetadata.metadataType)
            {
                case PlanningGISMetadataType.DATABASE_LEVEL:
                    metadataType = typeof(PlanningGISDatabaseMetadata);
                    break;
                //case PlanningGISMetadataType.REGION_LEVEL:
                //    metadataType = typeof(PlanningGISRegionMetadata);
                //    break;
                case PlanningGISMetadataType.REGION_LEVEL_DLG:
                    metadataType = typeof(PlanningGISDLGRegionMetadata);
                    break;
                case PlanningGISMetadataType.REGION_LEVEL_DEM:
                    metadataType = typeof(PlanningGISDEMRegionMetadata);
                    break;
                case PlanningGISMetadataType.REGION_LEVEL_DOM:
                    metadataType = typeof(PlanningGISDOMRegionMetadata);
                    break;
                case PlanningGISMetadataType.MAPSHEET_LEVEL:
                    metadataType = typeof(PlanningGISMapsheetMetadata);
                    break;
                case PlanningGISMetadataType.FEATURE_LEVEL:
                    metadataType = typeof(PlanningGISFeatureMetadata);
                    break;
            }

            FieldInfo fieldInfo = metadataType.GetField(pFieldName);

            if (fieldInfo.FieldType == typeof(DateTime))
            {
                DateTime dtTemp = new DateTime();

                if (DateTime.TryParse(pFieldValue, out dtTemp) == true)
                {
                    metadataType.InvokeMember(pFieldName
                    , BindingFlags.SetField
                    , null
                    , pMetadata
                    , new System.Object[] { dtTemp });
                }
                else
                {
                    pNote = "时间格式有误";
                    return false;
                }
            }
            else if (fieldInfo.FieldType == typeof(Int16))
            {
                Int16 intTemp;

                if (Int16.TryParse(pFieldValue, out intTemp) == true)
                {
                    metadataType.InvokeMember(pFieldName
                        , BindingFlags.SetField
                        , null
                        , pMetadata
                        , new System.Object[] { intTemp });
                }
                else
                {
                    pNote = "值非Int16类型";
                    return false;
                }
            }
            else if (fieldInfo.FieldType == typeof(Int32))
            {
                Int32 intTemp;

                if (Int32.TryParse(pFieldValue, out intTemp) == true)
                {
                    metadataType.InvokeMember(pFieldName
                        , BindingFlags.SetField
                        , null
                        , pMetadata
                        , new System.Object[] { intTemp });
                }
                else
                {
                    pNote = "值非Int32类型";
                    return false;
                }
            }
            else if (fieldInfo.FieldType == typeof(Int64))
            {
                Int64 intTemp;

                if (Int64.TryParse(pFieldValue, out intTemp) == true)
                {
                    metadataType.InvokeMember(pFieldName
                        , BindingFlags.SetField
                        , null
                        , pMetadata
                        , new System.Object[] { intTemp });
                }
                else
                {
                    pNote = "值非Int64类型";
                    return false;
                }
            }
            else if (fieldInfo.FieldType == typeof(Double))
            {
                Double doubleTemp;

                if (Double.TryParse(pFieldValue, out doubleTemp) == true)
                {
                    metadataType.InvokeMember(pFieldName
                        , BindingFlags.SetField
                        , null
                        , pMetadata
                        , new System.Object[] { doubleTemp });
                }
                else
                {
                    pNote = "值非Double类型";
                    return false;
                }
            }
            else if (fieldInfo.FieldType.BaseType == typeof(Enum))
            {
                Type type = pMetadata.GetType();

                if (EnumUtility.isOneOfEnumName(fieldInfo.FieldType
                    , pFieldValue) == true)
                {
                    metadataType.InvokeMember(pFieldName
                        , BindingFlags.SetField
                        , null
                        , pMetadata
                        , new System.Object[] { EnumUtility.getEnumByName(fieldInfo.FieldType
                                                ,pFieldValue) });
                }
                else
                {
                    pNote = "值非" + fieldInfo.FieldType.ToString() + "枚举类型";
                    return false;
                }
            }
            else
            {
                metadataType.InvokeMember(pFieldName
                        , BindingFlags.SetField
                        , null
                        , pMetadata
                        , new System.Object[] { pFieldValue });
            }

            pNote = "";
            return true;
        }
    }
}
